package top.majuan.study.study.dao;

import top.majuan.study.study.entity.Result;
import top.majuan.study.study.entity.TbUser;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TbUserDAO {
    public static final String REG = "insert into tb_user(username,password,userinfo) values(?,?,?)";
    public static final String QUERY_BY_NAME = "select * from tb_user where username=?";
    public static TbUser queryByName(String name) throws Exception {
        Connection conn = DBHelper.getConnection();
        TbUser user = null;
        PreparedStatement ps = conn.prepareStatement(QUERY_BY_NAME);
        ps.setString(1, name);
        ResultSet rs = ps.executeQuery();
        if (rs.next()){
            user = new TbUser();
            user.setId(rs.getInt("id"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setUserinfo(rs.getString("userinfo"));
            user.setReg_date(rs.getTimestamp("reg_date"));
        }
        conn.close();
        return user;

    }

    public static int reg(TbUser user) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(REG);
        ps.setString(1, user.getUsername());
        ps.setString(2, user.getPassword());
        ps.setString(3, user.getUserinfo());
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }


}
